package com.passwd.designsearch.search;


import com.passwd.designcore.utils.KeyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
public class SearchController {

    /**
     * 搜索方法得有参数，必须从网页访问。或者自己手动在url拼参content
     * @param content
     * @return
     */
    @RequestMapping("searchRun")
    public static String searchRun(String content) {
        String result = "error";
        try {

            String path = System.getProperty("user.dir");
            File dir = new File(path);
            //TODO 路径待写
            File file = new File(dir, KeyUtils.inputDecryptFileName);

            // 核心处理类
            SearchCore searchCore=new SearchCore();
            result = searchCore.searchRun(content,new FileInputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage();
        }
        return result;
    }

    /**
     * 上传文件方式搜索
     * @param file
     * @param content
     * @return
     * @throws Exception
     */
    @RequestMapping("searchUpload")
    public String encryptUpload(@RequestParam("file") MultipartFile file,String content) throws Exception {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        InputStream inputStream = file.getInputStream();

        // 核心处理类
        SearchCore searchCore=new SearchCore();
        String result = searchCore.searchRun(content,inputStream);
        return result;
    }

}
